package desktop;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.List;

class ChangeScore extends JFrame {
    ChangeScore(List<Screen> screens, List<ScoreTable> scoreTables, int N){ //赛局数
        JPanel jPanel_screen = new JPanel();
        JLabel jLabel_screen = new JLabel("请选择赛道：");
        JComboBox jComboBox_screen = new JComboBox();
        jComboBox_screen.addItem("--请选择--");
        for(int i=1;i<=screens.size();i++)
            jComboBox_screen.addItem("赛道"+i);
        jPanel_screen.add(jLabel_screen);
        jPanel_screen.add(jComboBox_screen);

        JPanel jPanel_team = new JPanel();
        JLabel jLabel_team = new JLabel("请选择队伍：");
        JRadioButton jRadioButton_red = new JRadioButton("红队",true);
        JRadioButton jRadioButton_yellow = new JRadioButton("黄队");
        ButtonGroup buttonGroup = new ButtonGroup();
        buttonGroup.add(jRadioButton_red);
        buttonGroup.add(jRadioButton_yellow);
        jPanel_team.add(jLabel_team);
        jPanel_team.add(jRadioButton_red);
        jPanel_team.add(jRadioButton_yellow);

        JPanel jPanel_match = new JPanel();
        JLabel jLabel_match = new JLabel("请选择赛局：");
        JComboBox jComboBox_match = new JComboBox();
        jComboBox_match.addItem("--请选择--");
        for(int i=1;i<=N;i++)
            jComboBox_match.addItem("赛局"+i);
        jPanel_match.add(jLabel_match);
        jPanel_match.add(jComboBox_match);

        JPanel jPanel_score = new JPanel();
        JLabel jLabel_score = new JLabel("输入新分数：");
        JTextField jTextField = new JTextField(5);
        jTextField.addKeyListener(new KeyAdapter() { //限制只能输入数字
            @Override
            public void keyTyped(KeyEvent e) {
                int keyChar = e.getKeyChar();
                if(keyChar < KeyEvent.VK_0 || keyChar > KeyEvent.VK_9)
                    e.consume(); //关键，屏蔽掉非法输入
            }
        });
        jPanel_score.add(jLabel_score);
        jPanel_score.add(jTextField);

        JButton jButton = new JButton("确定");
        final JFrame jFrame_this = this;
        jButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(jComboBox_screen.getSelectedIndex()==0)
                    JOptionPane.showMessageDialog(null,"请选择赛道！","警告",JOptionPane.WARNING_MESSAGE);
                else if(jComboBox_match.getSelectedIndex()==0)
                    JOptionPane.showMessageDialog(null,"请选择赛局！","警告",JOptionPane.WARNING_MESSAGE);
                else if(jTextField.getText().equals(""))
                    JOptionPane.showMessageDialog(null,"请输入分数！","警告",JOptionPane.WARNING_MESSAGE);
                else{
                    int screen = jComboBox_screen.getSelectedIndex();
                    System.out.println("选中的赛道："+screen);
                    boolean team = jRadioButton_red.isSelected();
                    System.out.println("选中的队伍："+(team?"红":"黄"));
                    int match = jComboBox_match.getSelectedIndex();
                    System.out.println("选中的赛局："+match);
                    int score = Integer.parseInt(jTextField.getText());
                    System.out.println("输入的分数："+score);
                    //修改计分屏上的分数
                    screens.get(screen-1).setScore(team,match,score);
                    //修改裁判系统主界面上的分数
                    scoreTables.get(screen-1).updateScore(team,match,score);
                    JOptionPane.showMessageDialog(null,"修改成功！","提示",JOptionPane.INFORMATION_MESSAGE);
                    jFrame_this.setVisible(false);
                }
            }
        });

        this.setLayout(new GridLayout(5,1));
        this.add(jPanel_screen);
        this.add(jPanel_team);
        this.add(jPanel_match);
        this.add(jPanel_score);
        this.add(jButton);
        this.setTitle("分数纠正");
        this.setSize(300,200);
        this.setResizable(false);
        this.setVisible(true);
    }
}
